﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using ServiceContracts;
using System.ServiceModel;

namespace Presention
{
    public partial class AuthorityList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAuthorityList();
            }
        }

        /// <summary>
        /// Binding AuthorityList DataSource
        /// </summary>
        private void GetAuthorityList()
        {
            using (ServiceAgent proxy = new ServiceAgent())
            {
                Reapeater1.DataSource = proxy.ServiceClient.GetAllAuthority();
                Reapeater1.DataBind();
                this.LevelDropDownList.DataSource = proxy.ServiceClient.GetAllAuthorityLevel();
                this.LevelDropDownList.DataTextField = "LevelName";
                this.LevelDropDownList.DataValueField = "LevelId";
                this.LevelDropDownList.DataBind();
                LevelDropDownList.Items.Insert(0, new ListItem("", ""));
            }
        }

        protected void CreateButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateNewAuthority.aspx");
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int id = int.Parse(btn.CommandArgument);
            Response.Redirect("UpdateAuthority.aspx?id=" + id);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int id = int.Parse(btn.CommandArgument);
            try
            {
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    proxy.ServiceClient.DeleteAuthority(id);
                    Response.Write("<script language=javascript>alert('Delete Successful！')</script>");
                    GetAuthorityList();
                }
            }
            catch (FaultException ex)
            {
                Response.Write("<script language=javascript>alert('"+ex.Message+"')</script>");
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            if (AuthorityNameTextBox.Text == "" && LevelDropDownList.Text == "")
            {
                GetAuthorityList();
            }
            else
            {
                string authorityname = AuthorityNameTextBox.Text;
                string levelname = LevelDropDownList.Text;
                using (ServiceAgent proxy = new ServiceAgent())
                {
                    Reapeater1.DataSource = proxy.ServiceClient.SearchAuthority(authorityname, levelname);
                    Reapeater1.DataBind();
                }
            }
        }
    }
}